


project, uses(../data_output/termlimited.dta)

	use ../data_output/termlimited.dta, clear

	gen l=1
	gen t = termlimited
	collapse (sum)  l t, by(state chamber term)
	reshape wide l t, i(state term) j(chamber) string
	*replace term = subinstr(term, "-","_",.)
	reshape wide *house* *senate* , i(term) j(state) string
	keep term *house* *senateME *senateAZ *senateLA
	egen lTotal = rowtotal(l*)
	egen tTotal = rowtotal(thouse* tsenate*)
	gen year=regexs(1) if regexm(term, "-([0-9]+)")
	destring year, replace
	
	
	quietly {
		cap log close
		set linesize 255
		log using "../tables_figures/legislators_by_state.tex", text replace
		
		noisily dis "\begin{table}[h]"
		noisily dis "\centering"
		noisily dis "\caption{\textbf{\#Term Limited Legislators / Total \# Legislators}\label{tab:legislators}"
		noisily dis	"}"
		noisily dis " \begin{adjustbox}{max width=\textwidth} "
		noisily dis "\begin{tabular}{l rrrr rrrr rrrr rrrr rr}"
		noisily dis "\toprule \toprule"
		
		noisily dis " Term  & AR   & AZ    & CA    & CO    & FL & ME & MI & MO & MT & NV &OH & OK &SD &LA  & AZ &LA  & ME & Total \\"
		noisily dis "   & House   & House    & House    & House  &House  & House & House &House &House & House & House &House & House &House & Senate & Senate & Senate & \\"

		noisily dis " \cline{2-18}    \\ " 

		gsort -term
		forval i=1/ 21 { 
			noisily dis  term[`i'] "&" %3.0f thouseAR[`i'] "/" lhouseAR[`i'] "&" %3.0f thouseAZ[`i'] "/" lhouseAZ[`i'] "&" 
			noisily dis				   %3.0f thouseCA[`i'] "/" lhouseCA[`i'] "&" %3.0f thouseCO[`i'] "/" lhouseCO[`i'] "&" 
			noisily dis				   %3.0f thouseFL[`i'] "/" lhouseFL[`i'] "&" %3.0f thouseME[`i'] "/" lhouseME[`i'] "&" 
			noisily dis	 			   %3.0f thouseMI[`i'] "/" lhouseMI[`i'] "&" %3.0f thouseMO[`i'] "/" lhouseMO[`i'] "&" 
			noisily dis				   %3.0f thouseMT[`i'] "/" lhouseMT[`i'] "&" %3.0f thouseNV[`i'] "/" lhouseNV[`i'] "&"
			noisily dis				   %3.0f thouseOH[`i'] "/" lhouseOH[`i'] "&" %3.0f thouseOK[`i'] "/" lhouseOK[`i'] "&"
			noisily dis        	       %3.0f thouseSD[`i'] "/" lhouseSD[`i'] "&" %3.0f thouseLA[`i'] "/" lhouseLA[`i'] "&"
			noisily dis				   %3.0f tsenateAZ[`i'] "/" lsenateAZ[`i'] "&" %3.0f tsenateLA[`i'] "/" lsenateLA[`i'] "&" 
			noisily dis				   %3.0f tsenateME[`i'] "/" lsenateME[`i'] "&" 
			noisily dis				   %3.0f tTotal[`i'] "/" lTotal[`i'] "\\[1mm]"
			
		}		
		
		collapse (sum) *house* *senate* *Total*

		forval i=1/ 1 { 
			noisily dis  	  "Total&" %3.0f thouseAR[`i'] "/" lhouseAR[`i'] "&" %3.0f thouseAZ[`i'] "/" lhouseAZ[`i'] "&" 
			noisily dis				   %3.0f thouseCA[`i'] "/" lhouseCA[`i'] "&" %3.0f thouseCO[`i'] "/" lhouseCO[`i'] "&" 
			noisily dis				   %3.0f thouseFL[`i'] "/" lhouseFL[`i'] "&" %3.0f thouseME[`i'] "/" lhouseME[`i'] "&" 
			noisily dis	 			   %3.0f thouseMI[`i'] "/" lhouseMI[`i'] "&" %3.0f thouseMO[`i'] "/" lhouseMO[`i'] "&" 
			noisily dis				   %3.0f thouseMT[`i'] "/" lhouseMT[`i'] "&" %3.0f thouseNV[`i'] "/" lhouseNV[`i'] "&"
			noisily dis				   %3.0f thouseOH[`i'] "/" lhouseOH[`i'] "&" %3.0f thouseOK[`i'] "/" lhouseOK[`i'] "&"
			noisily dis        	       %3.0f thouseSD[`i'] "/" lhouseSD[`i'] "&" %3.0f thouseLA[`i'] "/" lhouseLA[`i'] "&"
			noisily dis				   %3.0f tsenateAZ[`i'] "/" lsenateAZ[`i'] "&" %3.0f tsenateLA[`i'] "/" lsenateLA[`i'] "&"
			noisily dis				   %3.0f tsenateME[`i'] "/" lsenateME[`i'] "&" 			
			noisily dis			       %3.0f tTotal[`i'] "/" lTotal[`i'] "\\[1mm]"
			
		}			
		
		
		
		
		noisily dis "\bottomrule \bottomrule"	
		noisily dis "\multicolumn{10}{p{\textwidth}}{\footnotesize Louisiana has off-cycle elections, and legislators are elected for 4-year periods."
		noisily dis "  }"
		noisily dis "\end{tabular}"
		noisily dis "\end{adjustbox}"	
		noisily dis "\end{table}"
		
		log off
	}
	
	
